
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to use Django with uWSGI &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="How to use Django with Apache and mod_wsgi" href="modwsgi.html" />
    <link rel="prev" title="How to use Django with Gunicorn" href="gunicorn.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="gunicorn.html" title="How to use Django with Gunicorn">previous</a>
     |
    <a href="../../index.html" title="“How-to” guides" accesskey="U">up</a>
   |
    <a href="modwsgi.html" title="How to use Django with Apache and &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;mod_wsgi&lt;/span&gt;&lt;/code&gt;">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="howto-deployment-wsgi-uwsgi">
            
  <div class="section" id="s-how-to-use-django-with-uwsgi">
<span id="how-to-use-django-with-uwsgi"></span><h1>How to use Django with uWSGI<a class="headerlink" href="#how-to-use-django-with-uwsgi" title="Permalink to this headline">¶</a></h1>
<p><a class="reference external" href="https://uwsgi-docs.readthedocs.io/">uWSGI</a> is a fast, self-healing and developer/sysadmin-friendly application
container server coded in pure C.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The uWSGI docs offer a <a class="reference external" href="https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html">tutorial</a> covering Django, nginx, and uWSGI (one
possible deployment setup of many). The docs below are focused on how to
integrate Django with uWSGI.</p>
</div>
<div class="section" id="s-prerequisite-uwsgi">
<span id="prerequisite-uwsgi"></span><h2>Prerequisite: uWSGI<a class="headerlink" href="#prerequisite-uwsgi" title="Permalink to this headline">¶</a></h2>
<p>The uWSGI wiki describes several <a class="reference external" href="https://uwsgi-docs.readthedocs.io/en/latest/Install.html">installation procedures</a>. Using pip, the
Python package manager, you can install any uWSGI version with a single
command. For example:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">#</span> Install current stable version.
<span class="gp">$</span> pip install uwsgi

<span class="gp">#</span> Or install LTS <span class="o">(</span>long term support<span class="o">)</span>.
<span class="gp">$</span> pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
</pre></div>
</div>
<div class="section" id="s-uwsgi-model">
<span id="uwsgi-model"></span><h3>uWSGI model<a class="headerlink" href="#uwsgi-model" title="Permalink to this headline">¶</a></h3>
<p>uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache)
communicates with a <cite>django-uwsgi</cite> “worker” process to serve dynamic content.</p>
</div>
<div class="section" id="s-configuring-and-starting-the-uwsgi-server-for-django">
<span id="configuring-and-starting-the-uwsgi-server-for-django"></span><h3>Configuring and starting the uWSGI server for Django<a class="headerlink" href="#configuring-and-starting-the-uwsgi-server-for-django" title="Permalink to this headline">¶</a></h3>
<p>uWSGI supports multiple ways to configure the process. See uWSGI’s
<a class="reference external" href="https://uwsgi.readthedocs.io/en/latest/Configuration.html">configuration documentation</a>.</p>
<p>Here’s an example command to start a uWSGI server:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>uwsgi --chdir<span class="o">=</span>/path/to/your/project <span class="se">\</span>
    --module<span class="o">=</span>mysite.wsgi:application <span class="se">\</span>
    --env <span class="nv">DJANGO_SETTINGS_MODULE</span><span class="o">=</span>mysite.settings <span class="se">\</span>
    --master --pidfile<span class="o">=</span>/tmp/project-master.pid <span class="se">\</span>
    --socket<span class="o">=</span><span class="m">127</span>.0.0.1:49152 <span class="se">\ </span>     <span class="c1"># can also be a file</span>
    --processes<span class="o">=</span><span class="m">5</span> <span class="se">\ </span>                <span class="c1"># number of worker processes</span>
    --uid<span class="o">=</span><span class="m">1000</span> --gid<span class="o">=</span><span class="m">2000</span> <span class="se">\ </span>        <span class="c1"># if root, uwsgi can drop privileges</span>
    --harakiri<span class="o">=</span><span class="m">20</span> <span class="se">\ </span>                <span class="c1"># respawn processes taking more than 20 seconds</span>
    --max-requests<span class="o">=</span><span class="m">5000</span> <span class="se">\ </span>          <span class="c1"># respawn processes after serving 5000 requests</span>
    --vacuum <span class="se">\ </span>                     <span class="c1"># clear environment on exit</span>
    --home<span class="o">=</span>/path/to/virtual/env <span class="se">\ </span>  <span class="c1"># optional path to a virtualenv</span>
    --daemonize<span class="o">=</span>/var/log/uwsgi/yourproject.log      <span class="c1"># background the process</span>
</pre></div>
</div>
<p>This assumes you have a top-level project package named <code class="docutils literal notranslate"><span class="pre">mysite</span></code>, and
within it a module <code class="file docutils literal notranslate"><span class="pre">mysite/wsgi.py</span></code> that contains a WSGI <code class="docutils literal notranslate"><span class="pre">application</span></code>
object. This is the layout you’ll have if you ran <code class="docutils literal notranslate"><span class="pre">django-admin</span>
<span class="pre">startproject</span> <span class="pre">mysite</span></code> (using your own project name in place of <code class="docutils literal notranslate"><span class="pre">mysite</span></code>) with
a recent version of Django. If this file doesn’t exist, you’ll need to create
it. See the <a class="reference internal" href="index.html"><span class="doc">How to deploy with WSGI</span></a> documentation for the default
contents you should put in this file and what else you can add to it.</p>
<p>The Django-specific options here are:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">chdir</span></code>: The path to the directory that needs to be on Python’s import
path – i.e., the directory containing the <code class="docutils literal notranslate"><span class="pre">mysite</span></code> package.</li>
<li><code class="docutils literal notranslate"><span class="pre">module</span></code>: The WSGI module to use – probably the <code class="docutils literal notranslate"><span class="pre">mysite.wsgi</span></code> module
that <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">startproject</span></code></a> creates.</li>
<li><code class="docutils literal notranslate"><span class="pre">env</span></code>: Should probably contain at least <code class="docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code>.</li>
<li><code class="docutils literal notranslate"><span class="pre">home</span></code>: Optional path to your project virtualenv.</li>
</ul>
<p>Example ini configuration file:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">[</span>uwsgi<span class="o">]</span>
<span class="nv">chdir</span><span class="o">=</span>/path/to/your/project
<span class="nv">module</span><span class="o">=</span>mysite.wsgi:application
<span class="nv">master</span><span class="o">=</span>True
<span class="nv">pidfile</span><span class="o">=</span>/tmp/project-master.pid
<span class="nv">vacuum</span><span class="o">=</span>True
max-requests<span class="o">=</span><span class="m">5000</span>
<span class="nv">daemonize</span><span class="o">=</span>/var/log/uwsgi/yourproject.log
</pre></div>
</div>
<p>Example ini configuration file usage:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>uwsgi --ini uwsgi.ini
</pre></div>
</div>
<div class="admonition-fixing-unicodeencodeerror-for-file-uploads admonition">
<p class="first admonition-title">Fixing <code class="docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code> for file uploads</p>
<p>If you get a <code class="docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code> when uploading files with file names
that contain non-ASCII characters, make sure uWSGI is configured to accept
non-ASCII file names by adding this to your <code class="docutils literal notranslate"><span class="pre">uwsgi.ini</span></code>:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="nv">env</span> <span class="o">=</span> <span class="nv">LANG</span><span class="o">=</span>en_US.UTF-8
</pre></div>
</div>
<p class="last">See the <a class="reference internal" href="../../../ref/unicode.html#unicode-files"><span class="std std-ref">Files</span></a> section of the Unicode reference guide for
details.</p>
</div>
<p>See the uWSGI docs on <a class="reference external" href="https://uwsgi-docs.readthedocs.io/en/latest/Management.html">managing the uWSGI process</a> for information on
starting, stopping and reloading the uWSGI workers.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">How to use Django with uWSGI</a><ul>
<li><a class="reference internal" href="#prerequisite-uwsgi">Prerequisite: uWSGI</a><ul>
<li><a class="reference internal" href="#uwsgi-model">uWSGI model</a></li>
<li><a class="reference internal" href="#configuring-and-starting-the-uwsgi-server-for-django">Configuring and starting the uWSGI server for Django</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="gunicorn.html"
                        title="previous chapter">How to use Django with Gunicorn</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="modwsgi.html"
                        title="next chapter">How to use Django with Apache and <code class="docutils literal notranslate"><span class="pre">mod_wsgi</span></code></a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/howto/deployment/wsgi/uwsgi.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="gunicorn.html" title="How to use Django with Gunicorn">previous</a>
     |
    <a href="../../index.html" title="“How-to” guides" accesskey="U">up</a>
   |
    <a href="modwsgi.html" title="How to use Django with Apache and &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;mod_wsgi&lt;/span&gt;&lt;/code&gt;">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>